<?php
	require_once('start_session.php');
	require_once('global_vars.php');
	//if (!isset($_SESSION['user_id']))
		//exit("there is no user id");
	require_once('db_connect.php');
	$user_id = 'famous1234'; //$_SESSION['user_id'];
	$query = "SELECT address FROM members WHERE mId = '$user_id'";
	$result = mysqli_query($conn, $query)
		or die("$user_id로의 주소 얻어오는 쿼리 오류");
	$row = mysqli_fetch_array($result);
	$user_addr = $row['address'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>다음 지도API 테스트 페이지 </title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript" src="http://apis.daum.net/maps/maps2.js?apikey=fdedf555ead724e65234ad4eb1ccb6760c6a6f1a"></script>
<style type="text/css">
<!--
	#map_div {
		width: 1024px;
		height: 700px;
		border: 1px solid;
	}
	#map_canvas {
		width: 800px;
		height: 600px;
	}
	#thumb_img {
		width: 30px;
		height: 30px;
	}
	#mark_info {
		float: right;
	}
-->
</style>
<script type="text/javascript">
	var map;
	var geocoder;
	var thumbImage = [
		'<img src="./images/thumb_0.gif" id="thumb_img" name="thumb_img" />', // 오늘
		'<img src="./images/thumb_1.gif" id="thumb_img" name="thumb_img" />', // +1일 이후 모임
		'<img src="./images/thumb_2.gif" id="thumb_img" name="thumb_img" />', // +2일 이후 모임
		'<img src="./images/thumb_3.gif" id="thumb_img" name="thumb_img" />', // +3일 이후 모임
		'<img src="./images/thumb_4.gif" id="thumb_img" name="thumb_img" />', // 오늘 이전
	];
	function addThumbsToMap() {
	<?php
		// only in +3, -3 day
		$query = 'SELECT tID, latitude, longitude, (TO_DAYS(meetingtime) - TO_DAYS(now())) as datediff '.
						'FROM thumb WHERE meetingtime < DATE_ADD(now(), interval 3 day) '.
															'AND meetingtime > DATE_SUB(now(), interval 3 day)';
		$data = mysqli_query($conn, $query)
			or die("엄지데이터 가져오는 쿼리 오류");
		while ($row2 = mysqli_fetch_array($data)) {
			echo 'var markImg = thumbImage['.($row2['datediff'] >= 0 ? $row2['datediff'] : 4).'];';
			echo 'var point = new DLatLng('.$row2['latitude'].', '.$row2['longitude'].');'.
					'var option = {mark: markImg, '.
												'infowindow_mouseover: true, '.
												'infowindow: new DInfoWindow("http://'.HOME_PATH.'/get_thumb_info.php?is_detail=0&tID='.$row2['tID'].'", {width: 400, height: 300}) '.
												'};'.
					'var thumbMark = new DMark(point, option);'.
					'DEvent.addListener(thumbMark, "mousedown", function() {'.
							// 'var info = new DInfoWindow("http://'.HOME_PATH.'/.'",'.
																								// '{width: 340, height: 240, point: new DLatLng('.$row2['latitude'].', '.$row2['longitude'].')});'.
							// 'alert("http://'.HOME_PATH.'/get_thumb_info.php?tID='.$row2['tID'].'");'.
							// 'alert(point.getLat() + " " + point.getLng());'.
							// 'map.addOverlay(info);'.
							// 'infowindow.show();'.
							// 'window.open("get_thumb_info.php?is_detail=1&tID='.$row2['tID'].'", "엄지 상세 정보",'.
														// '"width=430, height=360, menubar=no, toolbar=no, location=no, scrollbars=yes, resizeable=no");'.
							'openPopup("get_thumb_info.php?is_detail=1&tID='.$row2['tID'].'", "thumb_info", 430, 360);'.
						'});'.
					'map.addOverlay(thumbMark);';
		}
	?>
	}
	
	function openPopup(path, name, width, height) {
		var left = (screen.width - width) / 2 ; 
		var top = (screen.height - height) / 2; 
		return window.open(path, name, "width=" + width + ", height=" + height + ", left=" + left + ",top=" + top + ",menubar=no, toolbar=no, modal=yes, dependent=yes");
	}
	
	function setMapWith(address) {
		geocoder.geocode( {'address': address }, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var validIndex;
				for (validIndex = 0; validIndex < results.length; validIndex++) {
					if (results[validIndex].geometry.location.lat() >=0 && results[validIndex].geometry.location.lng() >= 0) {
						//alert(results[validIndex].geometry.location.toString());
						break;
					}
				}
				if (validIndex >= results.length) {
					alert("검색결과가 없습니다");
					return;
				}
				var latlng = new DLatLng(results[validIndex].geometry.location.lat(), results[validIndex].geometry.location.lng());
				map.setCenter(latlng, 2);
			}
			else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
				alert("검색결과가 없습니다");
				return;
			}
			else
				alert("주소검색 중 네트워크 문제가 발생했습니다. 다시 입력해주세요");	
		});
	}
	function initialize() {
		var mapOption = {
			point: new DLatLng(37.4848000, 127.0383034),//37.5655342, 126.9384179),
			level: 5,
			map_type: "TYPE_SKYVIEW"
		};
		map = new DMap(document.getElementById("map_canvas"), mapOption);
		var zoomControl = new DZoomControl();
		var indexMapControl = new DIndexMapControl();
		map.addControl(zoomControl);
		zoomControl.setVAlign("middle");
		geocoder = new google.maps.Geocoder();
		var userAddr = <?php echo "'$user_addr'" ?>;
		//setMapWith(userAddr);
		document.getElementById("search_address").value = userAddr;
		addThumbsToMap();
		DEvent.addListener(map, "click", function(point) {
			openPopup("thumb_form.php?is_edit=0&from=map&lat=" + point.getLat() + "&lng=" + point.getLng(), "thumb_form", 800, 800);
		});
	}
</script>
</head>
<body onload="initialize()">
	<div id="map_div">
	<ul id="mark_info">
		<li><img src="./images/thumb_4.gif" id="thumb_img" name="thumb_img" />오늘 이전 모임</li>
		<li><img src="./images/thumb_0.gif" id="thumb_img" name="thumb_img" />오늘</li>
		<li><img src="./images/thumb_1.gif" id="thumb_img" name="thumb_img" />+1일 이후 모임</li>
		<li><img src="./images/thumb_2.gif" id="thumb_img" name="thumb_img" />+2일 이후 모임</li>
		<li><img src="./images/thumb_3.gif" id="thumb_img" name="thumb_img" />+3일 이후 모임</li>
	</ul>
	<div id="map_canvas" style="width:800px; height:600px"></div>
	<form id="map_search" action="#" onsubmit="setMapWith(this.search_address.value); return false;">
		<label for="search_address">지도검색: </label>
		<input type="text" id="search_address" name="search_address" size="80" />
		<input type="submit" value="검색" />
		<input type="text" name="return_data" id="return_data" onchange="alert('yeah');"/>
	</form>	
	</div>
</body>
</html>
<?php
	mysqli_close($conn);
?>